//
//  LWEditingResumeViewController.m
//  IOSResume
//
//  Created by wynn on 16/4/24.
//  Copyright © 2016年 wynn. All rights reserved.
//

#import "LWEditingResumeViewController.h"
#import "ACEExpandableTextCell.h"
#import "ResumeInfo.h"

#define kPadding 5

@interface LWEditingResumeViewController ()<ACEExpandableTableViewDelegate,UITableViewDelegate,UITableViewDataSource>
{
    CGFloat _cellHeight[2];
    CGFloat startMargin;
}
@property (nonatomic, strong) NSMutableArray *cellData;
@property (nonatomic, strong) UIButton *saveButton;
@property (nonatomic, strong) UIButton *cancelButton;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIImageView *backGroundImageView;

@end

@implementation LWEditingResumeViewController

- (NSMutableArray *)cellData
{
    if (!_cellData) {
        NSMutableArray *tempArray = [NSMutableArray arrayWithArray:@[_name, _sex, _nation, _age, _location, _education, _projects, _jops]];
        _cellData = tempArray;
    }
    return _cellData;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.updateText = self.oldText;
    
    if (!_backGroundImageView) {
        // 设置背景图片
        UIImage *backGroundImage = [UIImage imageNamed:@"defaultBackground"];
        self.backGroundImageView = [[UIImageView alloc] initWithImage:backGroundImage];
        self.backGroundImageView.frame = self.view.bounds;
        [self.view addSubview:self.backGroundImageView];
        
        [self initcancelButton];
        [self initSaveButton];
        self.tableView = [[UITableView alloc] init];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.frame = CGRectMake(0, startMargin * 3, SCREEN.width, SCREEN.height - startMargin * 2);
        [self.view addSubview:self.tableView];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table View Data Source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return self.cellData[self.index];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ACEExpandableTextCell *cell = [tableView expandableTextCellWithId:@"cellId"];
    cell.text = self.updateText;
    cell.textView.placeholder = self.cellData[self.index];
    return cell;
}


#pragma mark - Table View Delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return MAX(50.0, _cellHeight[indexPath.section]);
}

- (void)tableView:(UITableView *)tableView updatedHeight:(CGFloat)height atIndexPath:(NSIndexPath *)indexPath
{
    _cellHeight[indexPath.section] = height;
}

- (void)tableView:(UITableView *)tableView updatedText:(NSString *)text atIndexPath:(NSIndexPath *)indexPath
{
    self.updateText = text;
}

#pragma mark - 初始化取消按钮
- (void)initcancelButton
{
    if (!_cancelButton) {
        _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _cancelButton.frame = CGRectMake(startMargin, startMargin, startMargin, startMargin);
        [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelButton setTitleColor:MILK_WHITE forState:UIControlStateNormal];
        [_cancelButton setTitleColor:MILK_GRAY forState:UIControlStateHighlighted];
        [_cancelButton setBackgroundColor:MILK_RED];
        [_cancelButton addTarget:self action:@selector(cancelClick) forControlEvents:UIControlEventTouchUpInside];
    }
    
    [self.view addSubview:_cancelButton];
}

- (void)cancelClick
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - 初始化保存按钮
- (void)initSaveButton
{
    if (!_saveButton) {
        _saveButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _saveButton.frame = CGRectMake(SCREEN.width - startMargin - startMargin, startMargin, startMargin, startMargin);
        [_saveButton setTitle:@"保存" forState:UIControlStateNormal];
        [_saveButton setTitleColor:MILK_WHITE forState:UIControlStateNormal];
        [_saveButton setTitleColor:MILK_GRAY forState:UIControlStateHighlighted];
        [_saveButton setBackgroundColor:MILK_RED];
        [_saveButton addTarget:self action:@selector(saveClick) forControlEvents:UIControlEventTouchUpInside];
    }
    
    [self.view addSubview:_saveButton];
}

- (void)saveClick
{
    ResumeInfo *resumeInfo = [ResumeInfo shareModel];
    [resumeInfo.resumeInfoArray replaceObjectAtIndex:self.index withObject:self.updateText];
    [self dismissViewControllerAnimated:YES completion:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
